package com.example.dao;

import java.util.List;
import java.util.Map;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.example.domain.Employment;

/**
 * @author 袁小弘
 * @describe 就业登记DAO层接口
 * @date 2020-01-01
 */
@Repository
public interface EmploymentDao extends JpaRepository<Employment, Integer> {

    @Transactional
    @Modifying
    Integer deleteByEmploymentId(Integer employmentId);

    List<Employment> findByEmploymentId(Integer employmentId);

    @Transactional
    @Modifying
    Integer deleteByEmploymentIdIn(List<Integer> listId);

    @Query(nativeQuery = true, value = "SELECT u.user_Id as userId,u.account as userName,u.nickName,e.*\r\n" +
            "FROM users u INNER JOIN employment e ON u.user_Id = e.user_Id")
    List<Map<String, Object>> queryAllEmploymentAndUsersInfo();

    @Query(nativeQuery = true, value = "SELECT u.user_Id as userId,u.account as userName,u.nickName,e.*\r\n" +
            "FROM users u INNER JOIN employment e ON u.user_Id = e.user_Id\r\n" +
            "where e.status LIKE CONCAT('%',:status,'%')")
    List<Map<String, Object>> findByStatusLike(@Param("status") String status);

    @Query(nativeQuery = true, value = "SELECT u.user_Id as userId,u.account as userName,u.nickName,e.*\r\n" +
            "FROM users u INNER JOIN employment e ON u.user_Id = e.user_Id\r\n" +
            "where e.enterPrise LIKE CONCAT('%',:enterPrise,'%')")
    List<Map<String, Object>> findByEnterPriseLike(@Param("enterPrise") String enterPrise);

    @Query(nativeQuery = true, value = "SELECT u.user_Id as userId,u.account as userName,u.nickName,e.*\r\n" +
            "FROM users u INNER JOIN employment e ON u.user_Id = e.user_Id\r\n" +
            "where e.entryTime LIKE CONCAT('%',:entryTime,'%')")
    List<Map<String, Object>> findByEntryTimeLike(@Param("entryTime") String entryTime);

    @Query(nativeQuery = true, value = "SELECT u.user_Id as userId,u.account as userName,u.nickName,e.*\r\n" +
            "FROM users u INNER JOIN employment e ON u.user_Id = e.user_Id\r\n" +
            "where e.dimissionTime LIKE CONCAT('%',:dimissionTime,'%')")
    List<Map<String, Object>> findByDimissionTimeLike(@Param("dimissionTime") String dimissionTime);

    @Query(nativeQuery = true, value = "SELECT u.user_Id as userId,u.account as userName,u.nickName,e.*\r\n" +
            "FROM users u INNER JOIN employment e ON u.user_Id = e.user_Id\r\n" +
            "where u.account LIKE CONCAT('%',:userName,'%')")
    List<Map<String, Object>> findByUserNameLike(@Param("userName") String userName);

    @Query(nativeQuery = true, value = "SELECT u.user_Id as userId,u.account as userName,u.nickName,e.*\r\n" +
            "FROM users u INNER JOIN employment e ON u.user_Id = e.user_Id\r\n" +
            "where u.nickName LIKE CONCAT('%',:nickName,'%')")
    List<Map<String, Object>> findByNickNameLike(@Param("nickName") String nickName);

}
